package cn.iocoder.yudao.module.member.controller.admin.usercv;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.usercv.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercv.UserCvDO;
import cn.iocoder.yudao.module.member.service.usercv.UserCvService;

@Tag(name = "管理后台 - 用户简历管理")
@RestController
@RequestMapping("/member/user-cv")
@Validated
public class UserCvController {

    @Resource
    private UserCvService userCvService;

    @PostMapping("/create")
    @Operation(summary = "创建用户简历管理")
    @PreAuthorize("@ss.hasPermission('member:user-cv:create')")
    public CommonResult<Long> createUserCv(@Valid @RequestBody UserCvSaveReqVO createReqVO) {
        return success(userCvService.createUserCv(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户简历管理")
    @PreAuthorize("@ss.hasPermission('member:user-cv:update')")
    public CommonResult<Boolean> updateUserCv(@Valid @RequestBody UserCvSaveReqVO updateReqVO) {
        userCvService.updateUserCv(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户简历管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-cv:delete')")
    public CommonResult<Boolean> deleteUserCv(@RequestParam("id") Long id) {
        userCvService.deleteUserCv(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户简历管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-cv:query')")
    public CommonResult<UserCvRespVO> getUserCv(@RequestParam("id") Long id) {
        UserCvDO userCv = userCvService.getUserCv(id);
        return success(BeanUtils.toBean(userCv, UserCvRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户简历管理分页")
    @PreAuthorize("@ss.hasPermission('member:user-cv:query')")
    public CommonResult<PageResult<UserCvRespVO>> getUserCvPage(@Valid UserCvPageReqVO pageReqVO) {
        PageResult<UserCvDO> pageResult = userCvService.getUserCvPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserCvRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户简历管理 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-cv:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCvExcel(@Valid UserCvPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCvDO> list = userCvService.getUserCvPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户简历管理.xls", "数据", UserCvRespVO.class,
                        BeanUtils.toBean(list, UserCvRespVO.class));
    }

}